#!/bin/sh
#
# Hjälpscript till webgränsnitt för USB-backuper
#######################################

BACKUPLINE='rdiff-backup -v6 -b --force --exclude-special-files --exclude-regexp "\.$"'
#IN_FILE='../services/mondo/in'

###
# Kör backup
###
start_backup()
{
	device=`echo $1 | cut -d "^" -f 1`

	str=`grep " /media/usb." /proc/mounts`
	if echo $str | grep "^$device " > /dev/null; then
		mpoint=`echo $str | grep "$device" | cut -c 11-21`
		if [ ! -d $mpoint/bkp ]; then
			mkdir $mpoint/bkp
		fi
		#echo "" > $IN_FILE
		$BACKUPLINE --include /home/ --exclude /var/cache/ --exclude /var/run/ --exclude /var/tmp/ --include /var/ --exclude '**' / $mpoint/bkp/
	fi
}


###
# Hämtar från backup
###
restore_backup()
{
	mpoint=$1
	time=$3
	target=${@:$#:1}

	echo "RESTORE"

	if [ ! -d $target ]; then
		mkdir "$target"
	fi
	if [ -d $target ]; then
		for (( i=4; "$i" < "$#"; i++ )); do
			source=${@:$i:1}
			filename=`echo $source | grep -oE "[^/]*$"`
			echo "DO: $target$filename"
			rdiff-backup --force -r $time "$mpoint/bkp/$source" "$target$filename"
		done
		chmod --preserve-root -R 777 "$target"
		echo "DONE"
	else
		echo "ERROR: can't create backupdir"
	fi
}


###
# Raderar rdiff-backup -datakatalog
###
fix_backupdevice()
{
	device=`echo $1 | cut -d "^" -f 1`

	str=`grep " /media/usb." /proc/mounts`	
	if echo $str | grep "^$device " > /dev/null; then
		mpoint=`echo $str | grep "$device" | cut -c 11-21`
		if [ -d $mpoint/bkp/rdiff-backup-data ]; then
			rm --preserve-root -f -r $mpoint/bkp/rdiff-backup-data
		fi
	fi
}


###
# Listar alla usb-enheter
###
get_devlist()
{
	ret=1
	str=`grep " /media/usb." /proc/mounts`
	devices=`echo $str | cut -c 1-9`
	for device in $devices; do
		dev=`echo "$device" | cut -c 6-8`
		vendor=`cat /sys/block/$dev/device/vendor`
		model=`cat /sys/block/$dev/device/model`
		echo -e "$device^$vendor^$model"
		ret=0
	done
	return $ret
}


###
# kontrollerar anslutna usb-enheter tills någon hittas som är backupdisk (har 'rifnetbackup.txt'-fil)
###
get_backupdevice()
{
	str=`grep " /media/usb." /proc/mounts`
	devices=`echo $str | cut -c 1-9`
	for device in $devices; do
		dev=`echo "$device" | cut -c 6-8`
		mpoint=`echo $str | grep "$device" | cut -c 11-21`
		if [ -f $mpoint/rifnetbackup.txt ]; then
			vendor=`cat /sys/block/$dev/device/vendor`
			model=`cat /sys/block/$dev/device/model`
			echo -e "$device^$mpoint^$vendor^$model"
			return 0
		fi
	done
	return 1
}


###
# gör ansluten usb-enhet till backupdisk (rensar och ger den 'rifnetbackup.txt'-fil)
###
do_backupdevice()
{
	str=`grep " /media/usb." /proc/mounts`
	devices=`echo $str | cut -c 1-9`
	for device in $devices; do
		dev=`echo "$device" | cut -c 6-8`
		mpoint=`echo $str | grep "$device" | cut -c 11-21`
		if echo $mpoint | grep "^/media" > /dev/null; then
			rm --preserve-root -f -r $mpoint/*
		else
			echo "BUGG!!!";
			return 1;
		fi
		echo "Den här filen talar om för Uggla att enheten ska ses som en backup-enhet" > $mpoint/rifnetbackup.txt
		vendor=`cat /sys/block/$dev/device/vendor`
		model=`cat /sys/block/$dev/device/model`
		echo -e "$device^$vendor^$model"
		return 0
	done
	return 1;
}


###############

if [ $# == 0 ]; then
	echo "Skapat för anrop från webgränsnitt"
	echo "backup | fix | list | device | setdevice | restore"
	exit -1
fi



case "$1" in
backup)
	dev=`get_backupdevice`
	if [ ! "$dev" ]; then
		echo "not connected"
		../inc/sendmail "Backup kunde ej genomföras" "Den schemalagda backupen kunde ej köras då backuphårdisk inte kunde hittas.\n\nKontrollera att en extern USB-hårddisk är ansluten, den är igång och aktiverad som en backupenhet."
	else
		start_backup $dev
	fi
;;
restore)
	dev=`get_backupdevice`
	if [ ! "$dev" ]; then
		echo "not connected"
	else
		device=`echo $dev | cut -d "^" -f 1`
		str=`grep " /media/usb." /proc/mounts`
		if echo $str | grep "^$device " > /dev/null; then
			mpoint=`echo $str | grep "$device" | cut -c 11-21`
			restore_backup $mpoint "$@"
		fi
	fi
;;
fix)
	dev=`get_backupdevice`
	if [ ! "$dev" ]; then
		echo "not connected"
	else
		fix_backupdevice $dev
	fi
;;
list)
	if ! get_devlist; then
		echo "no connected"
	fi
;;
device)
	if ! get_backupdevice; then
		echo "not connected"
	fi
;;
setdevice)
	if ! do_backupdevice; then
		echo "no connected"
	fi
;;
esac

# Då vi jobbar med vfat
sync
